<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-select v-model="filters.cardState" placeholder="卡状态">
						<el-option
						  v-for="item in options_cardstate"
						  :label="item.label"
						  :value="item.value">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item>
					<el-select v-model="filters.cardType" placeholder="套餐">
						<el-option
						  v-for="item in options_cardtype"
						  :label="item.label"
						  :value="item.value">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item>
					<el-date-picker
					  v-model="filters.activeBt"
					  type="date"
					  placeholder="激活开始日期"
					  :picker-options="pickerOptions0">
					</el-date-picker>
				</el-form-item>
				<el-form-item>
					<el-date-picker
					  v-model="filters.activeEt"
					  type="date"
					  placeholder="激活截止日期"
					  :picker-options="pickerOptions0">
					</el-date-picker>
				</el-form-item>
				<el-form-item>
					<el-input v-model="filters.cardSegBt" placeholder="卡号段开始"></el-input>
				</el-form-item>
				<el-form-item>
					<el-input v-model="filters.cardSegEt" placeholder="卡号段截止"></el-input>
				</el-form-item>
				<el-form-item>
					<el-date-picker
					  v-model="filters.inSkuBt"
					  type="date"
					  placeholder="入库开始日期"
					  :picker-options="pickerOptions0">
					</el-date-picker>
				</el-form-item>
				<el-form-item>
					<el-date-picker
					  v-model="filters.inSkuEt"
					  type="date"
					  placeholder="入库截止日期"
					  :picker-options="pickerOptions0">
					</el-date-picker>
				</el-form-item>
				<el-form-item>
					<el-date-picker
					  v-model="filters.outSkuBt"
					  type="date"
					  placeholder="出库开始日期"
					  :picker-options="pickerOptions0">
					</el-date-picker>
				</el-form-item>
				<el-form-item>
					<el-date-picker
					  v-model="filters.outSkuEt"
					  type="date"
					  placeholder="出库截止日期"
					  :picker-options="pickerOptions0">
					</el-date-picker>
				</el-form-item>

				<el-form-item>
					<el-input v-model="filters.likeCardKey" placeholder="输卡号/ICCID/IMSI/查询码"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="getCards">查询</el-button>
				</el-form-item>
				
				<template v-if="sysUserName!='二级服务商'">
					<el-form-item>
						<el-button type="primary" @click="handleEdit">批量编辑</el-button>
					</el-form-item>
				</template>
				
				<el-form-item>
					<el-button type="primary" @click="exportExcel">导出EXCEL</el-button>
				</el-form-item>				
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="cards" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column prop="msisdn" label="卡号" width="120" sortable>
			</el-table-column>
			<el-table-column prop="iccid" label="ICCID" width="100" sortable>
			</el-table-column>
			<el-table-column prop="imsi" label="IMSI" width="100" sortable>
			</el-table-column>
			<el-table-column prop="querycode" label="查询代码" width="120" sortable>
			</el-table-column>
			<el-table-column prop="packagename" label="套餐名称" min-width="180" sortable>
			</el-table-column>
			<el-table-column prop="carstate" label="卡状态" min-width="100" sortable>
			</el-table-column>
			<el-table-column prop="activeDate" label="激活日期" min-width="120" sortable>
			</el-table-column>
			<el-table-column prop="addtime" label="入库日期" min-width="120" sortable>
			</el-table-column>
			<el-table-column prop="outSkuDate" label="出库日期" min-width="120" sortable>
			</el-table-column>			
			<el-table-column prop="servicername" label="服务商名称" min-width="180" sortable>
			</el-table-column>			
			<el-table-column prop="contactmobile" label="联系人" min-width="180" sortable>
			</el-table-column>	
			<el-table-column prop="currentmoney" label="用户余额" min-width="120" sortable>
			</el-table-column>	
			<el-table-column prop="totalgprs" label="总流量" min-width="100" sortable>
			</el-table-column>	
			<el-table-column prop="currentgprs" label="剩余流量" min-width="120" sortable>
			</el-table-column>	
			<el-table-column prop="usedsmsnum" label="短信使用条数" min-width="150" sortable>
			</el-table-column>				
			<el-table-column label="操作" width="100" fixed="right">
				<template scope="scope">
					<el-button size="small" @click="handleMore(scope.$index, scope.row)">详细</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条(分页)-->
		<el-col :span="24" class="toolbar">
			<!--
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			-->
			<el-pagination
			@size-change="handleSizeChange"
			@current-change="handleCurrentChange"
			:current-page="currentPage"
			:page-sizes="[10, 20, 50, 100]"
			:page-size="pageSize"
			layout="total, sizes, prev, pager, next, jumper"
			:total="total"
			style="float:right;">
			</el-pagination>
		</el-col>

		<!--批量编辑界面-->
		<el-dialog title="卡号批量编辑" v-model="editFormVisible" :close-on-click-modal="false">
			<el-form :model="editForm" label-width="100px" :rules="editFormRules" ref="editForm">
				<el-form-item label=" 卡 号 段：">
					<el-col :span="11">
					<el-form-item prop="cardnoBt">
						<el-input v-model="editForm.cardnoBt"  placeholder="请输入MSISDN" style="width: 100%;"></el-input>
					</el-form-item>
					</el-col>
					<el-col class="line" :span="2" style="text-align:center;">至</el-col>
					<el-col :span="11">
					<el-form-item prop="cardnoEt">
					  	<el-input v-model="editForm.cardnoEt"  placeholder="请输入MSISDN" style="width: 100%;" ></el-input>
					</el-form-item>
					</el-col>
				</el-form-item>
				<el-form-item label="套餐名称：">
					<el-col :span="1">
						<el-checkbox v-model="editForm.packagenameChecked"></el-checkbox>
					</el-col>
					<el-col :span="23">
							<el-select v-model="editForm.packagename" placeholder="请选择" style="width: 100%;">
								<el-option
								  v-for="item in options_cardstate"
								  :label="item.label"
								  :value="item.value">
								</el-option>
							</el-select>			
					</el-col>
				</el-form-item>
				<el-form-item label="API代码：">
					<el-col :span="1">
						<el-checkbox v-model="editForm.apiChecked"></el-checkbox>
					</el-col>
					<el-col :span="23">
							<el-select v-model="editForm.servicerapi" placeholder="请选择" style="width: 100%;">
								<el-option
								  v-for="item in options_cardstate"
								  :label="item.label"
								  :value="item.value">
								</el-option>
							</el-select>			
					</el-col>
				</el-form-item>
				<el-form-item label="服务商名：">
					<el-col :span="1">
						<el-checkbox v-model="editForm.servicemanChecked"></el-checkbox>
					</el-col>
					<el-col :span="23">
							<el-select v-model="editForm.serviceman" placeholder="请选择" style="width: 100%;">
								<el-option
								  v-for="item in options_cardstate"
								  :label="item.label"
								  :value="item.value">
								</el-option>
							</el-select>			
					</el-col>
				</el-form-item>				
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="editLoading">保存</el-button>
			</div>
		</el-dialog>

		<!--卡号信息详情界面-->
		<cardinfo ref="cardinfo"></cardinfo>
		
	</section>
</template>

<script>
	import util from '../../common/js/util'
	import constant from '../../common/js/constant'
	//import NProgress from 'nprogress'
	import { getCardListPage, removeUser, batchRemoveUser, editUser, addUser } from '../../api/api';
	import Cardinfo from '../../components/card_info.vue';

	export default {
		components: {
			cardinfo: Cardinfo
  		},
		data() {
			return {
			    sysUserName: '',
				pickerOptions0: {
					disabledDate(time) {
						return time.getTime() < Date.now() - 8.64e7;
					}
				},
				options_cardstate: constant.opt_cardstate,
				options_cardtype: constant.opt_cardtype,
				filters: {
					//卡状态
					cardState:'',
					//卡套餐名称类型
					cardType: '',
					//激活开始日期
					activeBt:'',
					//激活截至日期
					activeEt:'',
					//入库开始时间
					inSkuBt: '',
					//入库截止时间
					inSkuEt: '',
					//出库开始时间
					outSkuBt: '',
					//出库截止时间
					outSkuEt: '',					
					//卡号段开始
					cardSegBt: '',
				    //卡号段截止				
					cardSegEt: '',
					//模糊查询卡关键字
					likeCardKey: ''
				},
				cards: [],
				total: 0,//列表总条数
				pageSize:10,//每页条数
				currentPage:1,//当前页码
				listLoading: false,
				sels: [],//列表选中列

				editFormVisible: false,//批量编辑界面是否显示
				editLoading: false,
				editFormRules: {
					cardnoBt: [
						{ required: true, message: '请输入MSISDN', trigger: 'blur' }
					],
					cardnoEt: [
						{ required: true, message: '请输入MSISDN', trigger: 'blur' }
					]					
				},
				//批量编辑界面数据
				editForm: {
					cardnoBt: '',
					cardnoEt: '',
					packagenameChecked:true,
					packagename:'',
					apiChecked:true,
					servicerapi:'',
					servicemanChecked:true,
					serviceman:''
				}
		
			}
		},
		methods: {
			//性别显示转换
			formatSex: function (row, column) {
				return row.sex == 1 ? '男' : row.sex == 0 ? '女' : '未知';
			},
			selsChange: function (sels) {
				this.sels = sels;
			},
			exportExcel(){
				//location.href="/api/user/exportXls?pageSize="+this.pageSize+"&pageNum="+this.currentPage+"&userid="+123;
				location.href="/api/user/exportXls?userid="+123;
			},
			handleSizeChange(val) {
				this.pageSize = val;
				this.getCards();
			},
			handleCurrentChange(val) {
				this.currentPage = val;
				this.getCards();
			},
			//获取卡号列表
			getCards() {
				let para = {
					pageSize:this.pageSize,
					pageNum:this.currentPage			
				};
				para = Object.assign(para, this.filters);

				this.listLoading = true;
				//NProgress.start();
				getCardListPage(para).then((res) => {
					this.total = res.data.total;
					this.cards = res.data.list;
					this.listLoading = false;
					//NProgress.done();
				});
			},
			//显示卡号详细界面
			handleMore: function (index, row) {
				 this.$refs.cardinfo.showCardDetail(row.msisdn);
			},
			//显示批量编辑界面
			handleEdit: function (index, row) {
				this.editFormVisible = true;				
			},
			//编辑
			editSubmit: function () {
				this.$refs.editForm.validate((valid) => {
					if (valid) {
						alert(1);
						/*
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.editLoading = true;
							//NProgress.start();
							let para = Object.assign({}, this.editForm);
							para.birth = (!para.birth || para.birth == '') ? '' : util.formatDate.format(new Date(para.birth), 'yyyy-MM-dd');
							editUser(para).then((res) => {
								this.editLoading = false;
								//NProgress.done();
								this.$message({
									message: '提交成功',
									type: 'success'
								});
								this.$refs['editForm'].resetFields();
								this.editFormVisible = false;
								this.getCards();
							});
						});
						*/
					}
				});
			}
			
		},
		mounted() {
			this.getCards();
		}
	}

</script>